package com.qixin.modules.eaStandard.service.impl;

import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaStandard.entity.EaCommunicationstandard;
import com.qixin.modules.eaStandard.mapper.EaCommunicationstandardMapper;
import com.qixin.modules.eaStandard.service.IEaCommunicationstandardService;
import org.jeecg.common.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 通讯费标准
 * @Author: qixin-boot
 * @Date:   2021-07-17
 * @Version: V1.0
 */
@Service
public class EaCommunicationstandardServiceImpl extends ServiceImpl<EaCommunicationstandardMapper, EaCommunicationstandard> implements IEaCommunicationstandardService {
    @Autowired
    private EaCommunicationstandardMapper eaCommunicationstandardMapper;
    @Autowired
    private ChangeIdToNameUtil changeIdToNameUtil;

    /**
     * 保存
     * @param eaCommunicationstandard
     * @return map
     */
    public Map<String,Object> saveObject(EaCommunicationstandard eaCommunicationstandard){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        String companycode = EmptyUtil.checkNull(eaCommunicationstandard.getCompanycode());    // 组织
        String rank = EmptyUtil.checkNull(eaCommunicationstandard.getRankid());    // 级别

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("companycode",companycode);
        params.put("rankid",rank);
        List<EaCommunicationstandard> eaCommunicationstandardList = eaCommunicationstandardMapper.selectByMap(params);
        if(eaCommunicationstandardList.size() > 0){
            returnMap.put("code","-1");
            returnMap.put("message", "该组织" + changeIdToNameUtil.getNameByDepartId(companycode) + "对应的级别已经维护报销控制标准,不要重复维护!");
            return returnMap;
        }else{
            eaCommunicationstandardMapper.insert(eaCommunicationstandard);

            returnMap.put("code","0");
            returnMap.put("message","新增成功!");
            return returnMap;
        }
    }

    /**
     * 编辑
     * @param eaCommunicationstandard
     * @return map
     */
    public Map<String,Object> updateByIdObject(EaCommunicationstandard eaCommunicationstandard){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        //判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
        String companycode = EmptyUtil.checkNull(eaCommunicationstandard.getCompanycode());            //职务
        String rank = EmptyUtil.checkNull(eaCommunicationstandard.getRankid());              //职级

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("companycode",companycode);
        params.put("rankid",rank);

        List<EaCommunicationstandard> eaCommunicationstandardList = eaCommunicationstandardMapper.selectByMap(params);
        if(eaCommunicationstandardList.size() > 0){
            //查询当前标准
            EaCommunicationstandard eaCommunicationstandard1 = eaCommunicationstandardMapper.selectById(eaCommunicationstandard.getId());
            if(eaCommunicationstandard1.getCompanycode().equals(companycode) && eaCommunicationstandard1.getRankid().equals(rank)){  //如果当前标准维护的组织和职级没变,返回成功
                eaCommunicationstandardMapper.updateById(eaCommunicationstandard);
                returnMap.put("code","0");
                returnMap.put("message","编辑成功!");
                return returnMap;
            }else{
                returnMap.put("code","-1");
                returnMap.put("message","该组织:" + changeIdToNameUtil.getNameByDepartId(companycode) + "对应的职级已经维护报销控制标准,不要重复维护!");
                return returnMap;
            }
        }else{
            eaCommunicationstandardMapper.updateById(eaCommunicationstandard);
            returnMap.put("code","0");
            returnMap.put("message","编辑成功!");
            return returnMap;
        }

    }
}
